import 'package:flutter/material.dart';

//ListView列表组件
void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: HomePage());
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<HomePage> {
  final List<String> _list = [];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('StatefulWidget实现动态列表组件'),
        backgroundColor: const Color.fromARGB(255, 2, 209, 154),
      ),
      body: ListView(
        children: _list.map((item) {
          return ListTile(title: Text(item));
        }).toList(),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          //改变数据必须加setState
          setState(() {
            _list.add('列表项${_list.length + 1}');
          });
        },
      ),
    );
  }
}
